package com.acidu.blogs.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.acidu.blogs.domain.AddAciduBlogVo;
import com.acidu.blogtag.domain.AciduBlogTag;
import com.acidu.blogtag.mapper.AciduBlogTagMapper;
import com.acidu.blogtag.service.IAciduBlogTagService;
import com.acidu.tags.domain.AciduTag;
import com.acidu.tags.mapper.AciduTagMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.acidu.blogs.mapper.AciduBlogMapper;
import com.acidu.blogs.domain.AciduBlog;
import com.acidu.blogs.service.IAciduBlogService;

/**
 * 博文管理Service业务层处理
 * 
 * @author xiaofeng
 * @date 2025-01-21
 */
@Service
public class AciduBlogServiceImpl implements IAciduBlogService 
{
    @Autowired
    private AciduBlogMapper aciduBlogMapper;

    @Autowired
    private IAciduBlogTagService aciduBlogTagService;

    @Autowired
    private AciduBlogTagMapper aciduBlogTagMapper;

    @Autowired
    private AciduTagMapper aciduTagMapper;
    /**
     * 查询博文管理
     * 
     * @param id 博文管理主键
     * @return 博文管理
     */
    @Override
    public AciduBlog selectAciduBlogById(Long id)
    {
        AciduBlog aciduBlog = aciduBlogMapper.selectAciduBlogById(id);
        List<AciduTag> aciduTags = new ArrayList<>();
        AciduBlogTag aciduBlogTag1 = new AciduBlogTag();
        aciduBlogTag1.setBlogId(aciduBlog.getId());
        List<AciduBlogTag> aciduBlogTags = aciduBlogTagService.selectAciduBlogTagList(aciduBlogTag1);
        for (AciduBlogTag aciduBlogTag : aciduBlogTags) {
            AciduTag aciduTag = aciduTagMapper.selectAciduTagById(aciduBlogTag.getTagId());
            aciduTags.add(aciduTag);
        }
        aciduBlog.setBlogTags(aciduTags);

        return aciduBlog;
    }

    /**
     * 查询博文管理列表
     * 
     * @param aciduBlog 博文管理
     * @return 博文管理
     */
    @Override
    public List<AciduBlog> selectAciduBlogList(AciduBlog aciduBlog)
    {
        List<AciduBlog> aciduBlogs = aciduBlogMapper.selectAciduBlogList(aciduBlog);
        for (AciduBlog aciduBlog1 : aciduBlogs) {
            List<AciduTag> aciduTags = new ArrayList<>();
            AciduBlogTag aciduBlogTag1 = new AciduBlogTag();
            aciduBlogTag1.setBlogId(aciduBlog1.getId());
            List<AciduBlogTag> aciduBlogTags = aciduBlogTagService.selectAciduBlogTagList(aciduBlogTag1);
            for (AciduBlogTag aciduBlogTag : aciduBlogTags) {
                AciduTag aciduTag = aciduTagMapper.selectAciduTagById(aciduBlogTag.getTagId());
                aciduTags.add(aciduTag);
            }
            aciduBlog1.setBlogTags(aciduTags);
        }
        return aciduBlogs;
    }

    /**
     * 新增博文管理
     * 
     * @param aciduBlog 博文管理
     * @return 结果
     */
    @Override
    public int insertAciduBlog(AddAciduBlogVo aciduBlog)
    {
        aciduBlog.setCreateTime(DateUtils.getNowDate());
        int acidublog = aciduBlogMapper.insertAciduBlog(aciduBlog);
        for (String tagId : aciduBlog.getBlogTags()) {
            System.out.println("tagId:" + tagId);
            AciduBlogTag aciduBlogTag = new AciduBlogTag();
            aciduBlogTag.setBlogId(aciduBlog.getId());
            aciduBlogTag.setTagId(Long.valueOf(tagId));
            aciduBlogTagMapper.insertAciduBlogTag(aciduBlogTag);
        }
        return acidublog;
    }

    /**
     * 修改博文管理
     * 
     * @param aciduBlog 博文管理
     * @return 结果
     */
    @Override
    public int updateAciduBlog(AddAciduBlogVo aciduBlog)
    {
        aciduBlog.setUpdateTime(DateUtils.getNowDate());
        aciduBlogTagMapper.deleteAciduBlogTagByBlogId(aciduBlog.getId());
        List<String> blogTags = aciduBlog.getBlogTags();
        for (String tagId : blogTags) {
            AciduBlogTag aciduBlogTag = new AciduBlogTag();
            aciduBlogTag.setBlogId(aciduBlog.getId());
            aciduBlogTag.setTagId(Long.valueOf(tagId));
            aciduBlogTagMapper.insertAciduBlogTag(aciduBlogTag);
        }
        return aciduBlogMapper.updateAciduBlog(aciduBlog);
    }

    /**
     * 批量删除博文管理
     * 
     * @param ids 需要删除的博文管理主键
     * @return 结果
     */
    @Override
    public int deleteAciduBlogByIds(Long[] ids)
    {
        return aciduBlogMapper.deleteAciduBlogByIds(ids);
    }

    /**
     * 删除博文管理信息
     * 
     * @param id 博文管理主键
     * @return 结果
     */
    @Override
    public int deleteAciduBlogById(Long id)
    {
        return aciduBlogMapper.deleteAciduBlogById(id);
    }
}
